package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.WYAccountProjectMappingDaoI;
import sy.model.SysProjectInformationMapping;
import sy.model.WYAccountProjectMapping;
@Repository("wyAccountProjectMappingDao")
public class WYAccountProjectMappingDaoImpl implements WYAccountProjectMappingDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(WYAccountProjectMapping wyAccountProjectMapping) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(wyAccountProjectMapping);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from WYAccountProjectMapping where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public void modify(WYAccountProjectMapping wyAccountProjectMapping) {
		// TODO Auto-generated method stub
		this.getCurrentSession().update(wyAccountProjectMapping);
	}

	@Override
	public WYAccountProjectMapping getWYAccountProjectMappingById(String id) {
		// TODO Auto-generated method stub
		String hql = "from WYAccountProjectMapping where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("id",id);
		List<WYAccountProjectMapping> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WYAccountProjectMapping> listWYAccountProjectMapping() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from WYAccountProjectMapping").list();
	}

	@Override
	public List<WYAccountProjectMapping> listWYAccountProjectMappingByKey(
			String key) {
		// TODO Auto-generated method stub
		String hql = "from WYAccountProjectMapping where sysProject.id = :key or wyAccount = :key";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("key",key);
		List<WYAccountProjectMapping> list = query.list();
		return list;
	}
	
	@Override
	public List<WYAccountProjectMapping> listWYAccountProjectMappingByProjectAndAccount(String wyAccountId,String projectId) {
		// TODO Auto-generated method stub
		String hql = "from WYAccountProjectMapping where sysProject.id = :projectId and wyAccount = :wyAccountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("wyAccountId",wyAccountId);
		query.setString("projectId",projectId);
		List<WYAccountProjectMapping> list = query.list();
		return list;
	}

	@Override
	public void saveList(List<WYAccountProjectMapping> list) {
		// TODO Auto-generated method stub
		Session session = this.getCurrentSession();
		WYAccountProjectMapping wyAccountPro = null;
		for(int i = 0; i<list.size(); i++){
			wyAccountPro = list.get(i);
			session.save(wyAccountPro);
			if (i % 10 == 0) {  
				session.flush();  
				session.clear();  
			}  
		}
	}

	@Override
	public void deleteByWYAccountId(String wyAccountId) {
		// TODO Auto-generated method stub
		String hql = "delete from WYAccountProjectMapping where wyAccount.id = :wyAccountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("wyAccountId", wyAccountId);
		query.executeUpdate();
	}
}
